package com.yida.gwj.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.gwj.config.domain.GwjConfBankRate;
import com.yida.gwj.config.domain.dto.GwjConfBankRateDTO;
import com.yida.gwj.config.domain.request.GwjConfBankRateRequest;
import com.yida.gwj.config.domain.request.GwjConfigBankRateRequest;
import com.yida.gwj.config.mapper.GwjConfBankRateMapper;
import com.yida.gwj.config.service.IGwjConfBankRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 银行利率配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-01
 */
@Service
public class GwjConfBankRateServiceImpl implements IGwjConfBankRateService {
    @Autowired
    private GwjConfBankRateMapper gwjConfBankRateMapper;

    /**
     * 简易计算器允许最早时间
     */
    private static final String earliestTime = "1998-07-01";


    /**
     * 查询银行利率配置
     *
     * @param id 银行利率配置主键
     * @return 银行利率配置
     */
    @Override
    public GwjConfBankRate selectGwjConfBankRateById(Long id) {
        return gwjConfBankRateMapper.selectGwjConfBankRateById(id);
    }

    /**
     * 查询银行利率配置列表
     *
     * @param gwjConfBankRate 银行利率配置
     * @return 银行利率配置
     */
    @Override
    public List<GwjConfBankRate> selectGwjConfBankRateList(GwjConfBankRate gwjConfBankRate) {
        return gwjConfBankRateMapper.selectGwjConfBankRateList(gwjConfBankRate);
    }

    /**
     * 新增银行利率配置
     *
     * @param request 银行利率配置
     * @return 结果
     */
    @Override
    public int insertGwjConfBankRate(GwjConfigBankRateRequest request) {
        GwjConfBankRate gwjConfBankRate = BeanUtil.copyProperties(request, GwjConfBankRate.class);
        gwjConfBankRate.setId(null);
        gwjConfBankRate.setCreateTime(DateUtils.getNowDate());
        return gwjConfBankRateMapper.insertGwjConfBankRate(gwjConfBankRate);
    }

    /**
     * 修改银行利率配置
     *
     * @param request 银行利率配置
     * @return 结果
     */
    @Override
    public int updateGwjConfBankRate(GwjConfigBankRateRequest request) {
        GwjConfBankRate gwjConfBankRate = BeanUtil.copyProperties(request, GwjConfBankRate.class);
        gwjConfBankRate.setUpdateTime(DateUtils.getNowDate());
        return gwjConfBankRateMapper.updateGwjConfBankRate(gwjConfBankRate);
    }

    /**
     * 批量删除银行利率配置
     *
     * @param ids 需要删除的银行利率配置主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfBankRateByIds(Long[] ids) {
        return gwjConfBankRateMapper.deleteGwjConfBankRateByIds(ids);
    }

    /**
     * 删除银行利率配置信息
     *
     * @param id 银行利率配置主键
     * @return 结果
     */
    @Override
    public int deleteGwjConfBankRateById(Long id) {
        return gwjConfBankRateMapper.deleteGwjConfBankRateById(id);
    }

    @Override
    public GwjConfBankRateDTO calculateSimpleRate(GwjConfBankRateRequest request) {
        //存储时间校验
        if (request.getStartTime().isBefore(LocalDate.parse(earliestTime))) {
            throw new ServiceException("对不起，该工具只能从1998-07-01开始计算利息");
        }
        //间隔天数
        long days = request.getStartTime().until(request.getEndTime(), ChronoUnit.DAYS);
        //年利率
        BigDecimal yearRate = request.getRate().divide(new BigDecimal("100")).setScale(4, BigDecimal.ROUND_HALF_UP);
        //日利率 一年以360天为标准
        BigDecimal dayRate = new BigDecimal((Double.toString((double) days / 360))).multiply(yearRate).setScale(4, BigDecimal.ROUND_HALF_UP);

        GwjConfBankRateDTO dto = new GwjConfBankRateDTO();
        dto.setDays(days);
        dto.setEarnedRateMoney(request.getInitSaveMoney().multiply(dayRate).setScale(2, BigDecimal.ROUND_HALF_UP));
        dto.setTotalMoney(request.getInitSaveMoney().add(dto.getEarnedRateMoney()).setScale(2, BigDecimal.ROUND_HALF_UP));
        return dto;
    }
}
